#include "CollisionTestScreen.h"

CollisionTestScreen::CollisionTestScreen() : m_pKeyboard(NULL), m_pController(NULL), m_world(NULL), m_pPlayer(NULL)
{
	m_world			= new GameWorld();
	m_pController	= g_pDInput->makeController();
	m_pKeyboard		= g_pDInput->makeKeyboard();

	D3DXVECTOR3 eyePosition, targetPosition, vectorUp;
	eyePosition		= D3DXVECTOR3(320.0F, 400.0F, -1000.0F);
	targetPosition	= D3DXVECTOR3(320.0F, 400.0F, 0.0F);
	vectorUp		= D3DXVECTOR3(0.0F, 1.0F, 0.0F);

	m_pCamera		= new cCamera2D(eyePosition, targetPosition, vectorUp);
	m_pCamera->BuildCameraProjection();
	m_pCamera->BuildCameraView();

	m_pPlayer		= m_world->getPlayer();
	m_pRock			= m_world->getRock();
	m_pCurrentRoom	= m_world->getRoom();
	m_pGfxStats		= new GfxStats();
}

CollisionTestScreen::~CollisionTestScreen()
{
	m_pPlayer		= NULL;
	m_pCurrentRoom	= NULL;
	delete m_pKeyboard;
	delete m_pController;
	delete m_world;
	delete m_pGfxStats;
}

void CollisionTestScreen::updateScreen(float dt)
{
	//Get the new instance of the player
	m_world->updateWorld(dt);
	updatePlayer(dt);
	m_pGfxStats->update(dt);
}

void CollisionTestScreen::renderScreen()
{
	//Starts the sprite rendering process
	g_pGraphics->beginSpriteRender(D3DXSPRITE_OBJECTSPACE | D3DXSPRITE_DONOTMODIFY_RENDERSTATE | D3DXSPRITE_ALPHABLEND);

	//Renders the Room information
	m_pCurrentRoom->renderRoom();

	//Controls the Affine Movement of the objects
	D3DXMATRIX trans, rotation, scale;
	D3DXMatrixIdentity(&trans);
	D3DXMatrixIdentity(&rotation);
	D3DXMatrixIdentity(&scale);

	//Controls the placements of the viewable objects
	D3DXMATRIX view;
	D3DXMatrixIdentity(&view);

	float posX = m_pPlayer->getPosX();
	float posY = m_pPlayer->getPosY();

	D3DXMatrixRotationZ(&rotation, D3DXToRadian(m_pPlayer->getOrientation()));
	D3DXMatrixTranslation(&trans, posX, posY, 0.0f);
	D3DXMatrixMultiply(&view, &rotation, &trans);
	g_pGraphics->SetTranformation(view);
 
	g_pGraphics->drawSpriteC(g_pAssets->m_pEzreal_Idle, m_pPlayer->getCenter());

	D3DXMatrixIdentity(&trans);

	D3DXMatrixTranslation(&trans, m_pRock->getPosX(), m_pRock->getPosY(), 0.0f);
	g_pGraphics->SetTranformation(trans);

	g_pGraphics->drawSprite(g_pAssets->m_pRock);

	g_pD3DDevice->SetTransform(D3DTS_WORLD,&(m_pCamera->m_view * m_pCamera->m_projection));

	//Flush the batched sprites to the screen
	g_pGraphics->flushSprites();

	//End the sprite rendering process
	g_pGraphics->endSpriteRender();

	m_pGfxStats->displayStats();
}

void CollisionTestScreen::onResetDevice()
{
	g_pGraphics->onResetDevice();
}

void CollisionTestScreen::onLostDevice()
{
	g_pGraphics->onLostDevice();
}

void CollisionTestScreen::updatePlayer(float dt)
{
	//Controls the information regarding the speed
	float max_speed = (5.0f * MAX_FPS); 

	float posX = 0.0f, posY = 0.0f;

	posX = m_pPlayer->getPosX();
	posY = m_pPlayer->getPosY();
	
	if(m_pController->isConnected())
	{ 
		//Player Movement with the Controller
		if(m_pController->getState().Gamepad.sThumbLX > XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE)
		{
			m_pPlayer->setOrientation(TURN_UP);
			posX += 0.50f;
		}

		else if(m_pController->getState().Gamepad.sThumbLX < -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE)
		{
			m_pPlayer->setOrientation(TURN_DOWN);
			posX -= 0.50f;
		}

		if(m_pController->getState().Gamepad.sThumbLY > XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE)
		{
			m_pPlayer->setOrientation(TURN_LEFT);
			posY += 0.50f;
		}

		else if(m_pController->getState().Gamepad.sThumbLY < -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE)
		{
			m_pPlayer->setOrientation(TURN_RIGHT);
			posY -= 0.50f;
		}

		//Fire Projectile
		if(m_pController->getState().Gamepad.sThumbRX > XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE)
		{
			
		}

		else if(m_pController->getState().Gamepad.sThumbRX < -XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE)
		{

		}

		if(m_pController->getState().Gamepad.sThumbRY > XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE)
		{

		}

		else if(m_pController->getState().Gamepad.sThumbRX < -XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE)
		{

		}

		//Switch the currently selected ability
		if(m_pController->getState().Gamepad.wButtons == XINPUT_GAMEPAD_A)
		{
		}

		else if(m_pController->getState().Gamepad.wButtons == XINPUT_GAMEPAD_B)
		{
		}

		else if(m_pController->getState().Gamepad.wButtons == XINPUT_GAMEPAD_X)
		{
		}

		else if(m_pController->getState().Gamepad.wButtons == XINPUT_GAMEPAD_Y)
		{
		}
	}

	if(m_pKeyboard != NULL)
	{
		m_pKeyboard->Poll();

		//Player Movement
		if(m_pPlayer->canMove())
		{
			if(m_pPlayer->m_bMoveUp)
			{
				//Moves the player up
				if(m_pKeyboard->isKeyPressed(DIK_W))
				{
					m_pPlayer->setOrientation(TURN_UP);
					posY += 0.50f;
				}
			}

			//Move the player down
			if(m_pPlayer->m_bMoveDown)
			{
				if(m_pKeyboard->isKeyPressed(DIK_S))
				{
					m_pPlayer->setOrientation(TURN_DOWN);
					posY -= 0.50f;
				}
			}

			//Move the player left
			if(m_pPlayer->m_bMoveLeft)
			{
				if(m_pKeyboard->isKeyPressed(DIK_A))
				{
					m_pPlayer->setOrientation(TURN_LEFT);
					posX -= 0.50f;
				}
			}
			//Move the player right
			if(m_pPlayer->m_bMoveRight)
			{
				if(m_pKeyboard->isKeyPressed(DIK_D))
				{
					m_pPlayer->setOrientation(TURN_RIGHT);
					posX += 0.50f;
				}
			}
		}

		//Player Attack
		if(m_pKeyboard->isKeyPressed(DIK_UP))
		{
			//m_pPlayer->fireProjectile();
		}

		else if(m_pKeyboard->isKeyPressed(DIK_DOWN))
		{
			//m_pPlayer->fireProjectile();
		}

		if(m_pKeyboard->isKeyPressed(DIK_LEFT))
		{
			//m_pPlayer->fireProjectile();
		}

		else if(m_pKeyboard->isKeyPressed(DIK_RIGHT))
		{
			//m_pPlayer->fireProjectile();
		}
	}

	Vector3D position = Vector3D(posX, posY, 0.0f);
	m_pPlayer->setPosition(position);
}